﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using Besture.UI.Admin.Master;


namespace Besture.UI.Admin.CommonModel
{
    public partial class FieldAdd : BasePage
    {

        public string _ModelID = string.Empty;

        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            _ModelID = Query("ModelID");

            ViewState["ModelID"] = _ModelID;
            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);

            ((ConfigAdmin)this.Master).Title = "内容模型管理";
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {

        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {

            ContentModelFieldModel model = new ContentModelFieldModel();

            model.ModelID = Convert.ToInt32(ViewState["ModelID"]);
            model.Name = txtName.Text;
            model.Alias = txtAlias.Text;
            model.Tips = txtTips.Text;
            model.FieldType = Convert.ToInt32(Request.Form["rdFieldType"]);
            model.MaxLength = Convert.ToInt32(txtMaxLength.Text);
            model.TextBoxWidth = Convert.ToInt32(txtBoxLength.Text);
            model.ValidateRule = ddlValidate.SelectedValue;
            model.DefaultValue = txtDefaultValue.Text;
            model.DisWidth = Convert.ToInt32(txtDisWidth.Text);
            model.DisHeight = Convert.ToInt32(txtDisHeight.Text);
            model.OptionValue = txtOptionValue.Text;
            model.OptionType = Convert.ToInt32(Request.Form["rdOptionDisType"]);
            model.PhotoType = txtPhotoType.Text;
            model.PhotoPath = txtPhotoPath.Text;
            model.FileType = txtFileType.Text;
            model.FilePath = txtFilePath.Text;
            model.IsDefaultTime = Convert.ToInt32(cbDefaultTime.Checked);
            model.ListDisplay = Convert.ToInt32(ddlListDisplay.SelectedValue);
            //必输值
            model.Required = Convert.ToInt32(cbRequired.Checked);

            //添加列
            string tableName =
                QueryDac.GetTable("SELECT TableName FROM TBContentModel WHERE ID = " + model.ModelID).Rows[0][
                    "TableName"].ToString();

            string addColumnSql = AddColumnSql(tableName, model);


            ContentModelFieldDac dac = new ContentModelFieldDac();
            dac.Add(model, addColumnSql);

            FunHelper.Alert("添加成功", "FieldList.aspx?ModelID=" + model.ModelID);

        }


        public string AddColumnSql(string tableName ,ContentModelFieldModel model)
        {
            string addColumnSqlText = string.Empty;

            
            switch (model.FieldType)
            {
                //单行文本
                case 1:
                    addColumnSqlText = string.Format(@"ALTER TABLE {0} ADD {1} nvarchar({2})", tableName, model.Name, model.MaxLength);
                    break;
                //多行文本
                case 2:
                    addColumnSqlText = string.Format(@"ALTER TABLE {0} ADD {1} text", tableName, model.Name);
                    break;
                //富文本
                case 3:
                    addColumnSqlText = string.Format(@"ALTER TABLE {0} ADD {1} text", tableName, model.Name);
                    break;
                //选项
                case 4:
                    addColumnSqlText = string.Format(@"ALTER TABLE {0} ADD {1} text", tableName, model.Name);
                    break;
                //图片
                case 5:
                    addColumnSqlText = string.Format(@"ALTER TABLE {0} ADD {1} nvarchar(500)", tableName, model.Name);
                    break;
                //文件
                case 6:
                    addColumnSqlText = string.Format(@"ALTER TABLE {0} ADD {1} nvarchar(500)", tableName, model.Name);
                    break;
                case 7:
                    addColumnSqlText = string.Format(@"ALTER TABLE {0} ADD {1} DATETIME", tableName, model.Name);
                    break;
            }
            return addColumnSqlText;
        }

        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {

        }

        #endregion
    }
}